package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReportQuery;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesx01Intfsource;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesx03Intflog;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class FileContentsQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,Object[] parameters) {
            	
		Teesx01Intfsource eesx01Intfsource = (Teesx01Intfsource) parameters[0];
		Timestamp fechaDesde = (Timestamp) parameters[1];
		Timestamp fechaHasta = (Timestamp) parameters[2];
        String status = (String) parameters[3];  
        
        Expression expFinal = null;
        
        
        if(parameters.length>0){
        
	        if (status!=null) {
	        	
	        	boolean notIn = false;
	        	
			    ExpressionBuilder inExpression = new ExpressionBuilder(); // crea otro expression builder
				
				Expression exp = null;
				
				exp = inExpression.get("teesx02Fileconten").get("eesx01Intfsource").get("eesx01IntfIdNumberK").equal(builder.get("eesx01Intfsource").get("eesx01IntfIdNumberK"));
				exp = exp.and(inExpression.get("teesx02Fileconten").get("eesx02FconSequenceK").equal(builder.get("eesx02FconSequenceK"))); 
				
				if (status == "P") {
				   exp = exp.and(inExpression.get("eesx03IlogProcStatusCodeC").equal("S"));
				   notIn = true;
				}
				else {
					notIn = false;
					exp = exp.and(inExpression.get("eesx03IlogProcStatusCodeC").equal(status));
				}
				
				ReportQuery report = new ReportQuery(Teesx03Intflog.class, exp);
				report.addAttribute("teesx03IntfIdNumberR", inExpression.get(
						"teesx02Fileconten").get("eesx01Intfsource").get("eesx01IntfIdNumberK"));
				report.addAttribute("eesx03FconSequenceR", inExpression.get(
						"teesx02Fileconten").get("eesx02FconSequenceK"));
			
				if (notIn) {
					expFinal = builder.exists(report).not();	
				} else {
					expFinal = builder.exists(report);
				}
				
	        }
	        
	        if (eesx01Intfsource != null) {
	        	if (expFinal != null) {
	        		expFinal = expFinal.and(builder.get("eesx01Intfsource").get("eesx01IntfIdNumberK").equal(eesx01Intfsource.getEesx01IntfIdNumberK()));
	        	}
	        	else {
	        		expFinal = builder.get("eesx01Intfsource").get("eesx01IntfIdNumberK").equal(eesx01Intfsource.getEesx01IntfIdNumberK()); 
	        	}
	        }
	        
	        if (fechaDesde != null) {
	        	if (expFinal != null) {
	        		expFinal = expFinal.and(builder.get("eesx02FconGenerationDateM").greaterThan(fechaDesde));
	        	}
	        	else {
	        		expFinal = builder.get("eesx02FconGenerationDateM").greaterThan(fechaDesde);
	        	}
	        }
	        
	        if (fechaHasta != null) {
	        	if (expFinal != null) {
	        		expFinal = expFinal.and(builder.get("eesx02FconGenerationDateM").lessThan(fechaHasta));
	        	}
	        	else {
	        		expFinal = builder.get("eesx02FconGenerationDateM").lessThan(fechaHasta);
	        	}
	        }
	    
        }
		
		return expFinal;
    	
    }

}